package com.qf.service.impl;

import com.qf.client.ShopCartClient;
import com.qf.dao.ShopMapper;
import com.qf.dao.ShopRepository;
import com.qf.pojo.BaseResp;
import com.qf.pojo.Toys;
import com.qf.service.ShopService;
import com.qf.utils.CookieUtils;
import com.qf.utils.JWTutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapaper;

    @Autowired
    ShopRepository shopRepository;

    @Autowired
    ShopCartClient shopCartClient;

    @Autowired
    RedisTemplate redisTemplate;

    private static final String MY_LIKE="my_like_";



    @Override
    public BaseResp findAll() {
        BaseResp baseResp = new BaseResp();
        List<Toys> all = shopMapaper.findAll();
        if (all!=null){
            baseResp.setCode(200);
            baseResp.setMessage("查询成功");
            baseResp.setData(all);
            return baseResp;
        }
        baseResp.setCode(-1);
        baseResp.setMessage("查询失败");
        return baseResp;
    }

    @Override
    public BaseResp findById(Integer id) {
        BaseResp baseResp = new BaseResp();
        Optional<Toys> byId = shopRepository.findById(id);
        if (byId.isPresent()){
            baseResp.setCode(200);
            baseResp.setData(byId.get());
            return baseResp;
        }
        baseResp.setCode(-1);
        baseResp.setMessage("查询单个失败");
        return baseResp;
    }

    @Override
    public BaseResp addShopCart(Integer id, Integer num, HttpServletRequest request) {
        Optional<Toys> byId = shopRepository.findById(id);
        BaseResp baseResp = null;
        if (byId.isPresent()){
            Map map = new HashMap();
            map.put("shop",byId);
            map.put("num",num);
            Integer id1 = getId(request);
            map.put("userid",id1);
            baseResp = shopCartClient.addShopCart(map);
            return baseResp;
        }else{
            baseResp.setMessage("商品下架");
            baseResp.setCode(2003);
        }
        return baseResp;
    }

    @Override
    public BaseResp addMyLike(Integer id, HttpServletRequest request) {
        Optional<Toys> byId = shopRepository.findById(id);
        BaseResp baseResp = new BaseResp();
        if (byId.isPresent()){
            Integer id1 = getId(request);
            //先在redis中查询是否有该用户的收藏信息 有则返回 已收藏 无则添加
            Object o = redisTemplate.opsForHash().get(MY_LIKE + id1.toString(), id.toString());
            if (o!=null){
                baseResp.setMessage("已收藏");
                baseResp.setCode(201);
                return baseResp;
            }
            //将 mylike+userid shopid shop详情 存入 redis中
            redisTemplate.opsForHash().put(MY_LIKE+id1.toString(),id.toString(),byId.get());
            Toys toys = byId.get();
            toys.setFavorite(toys.getFavorite()+1);
            shopRepository.saveAndFlush(toys);
            baseResp.setMessage("收藏成功");
            baseResp.setCode(200);
            return baseResp;
        }
        return null;
    }

    @Override
    public BaseResp findMyLike(HttpServletRequest request) {
        Integer id1 = getId(request);
        List values = redisTemplate.opsForHash().values(MY_LIKE+id1.toString());
        BaseResp baseResp = new BaseResp();
        baseResp.setData(values);
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp deleteMylike(String id,HttpServletRequest request) {
        Integer id1 = getId(request);
        redisTemplate.opsForHash().delete(MY_LIKE+id1.toString(),id);
        Optional<Toys> byId = shopRepository.findById(Integer.valueOf(id));
        Toys toys = byId.get();
        toys.setFavorite(toys.getFavorite()-1);
        shopRepository.saveAndFlush(toys);
        BaseResp baseResp = new BaseResp();
        baseResp.setMessage("取消收藏成功");
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp deleteAllMylike(String idlist, HttpServletRequest request) {
        Integer id = getId(request);
        String substring = idlist.substring(0, idlist.length() - 1);
        String[] split = substring.split(",");
        for (int i=0;i<split.length;i++){
            redisTemplate.opsForHash().delete(MY_LIKE+id,split[i]);
            Optional<Toys> byId = shopRepository.findById(Integer.valueOf(split[i]));
            Toys toys = byId.get();
            toys.setFavorite(toys.getFavorite()-1);
            shopRepository.saveAndFlush(toys);
        }
        BaseResp baseResp = new BaseResp();
        baseResp.setMessage("取消收藏成功");
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp findCollect(String id, HttpServletRequest request) {
        Integer id1 = getId(request);
        Boolean aBoolean = redisTemplate.opsForHash().hasKey(MY_LIKE + id1.toString(), id);
        BaseResp baseResp = new BaseResp();
        if (aBoolean){
            baseResp.setMessage("商品已被收藏");
            baseResp.setCode(200);
            return baseResp;
        }else{
            baseResp.setMessage("商品未被收藏");
            baseResp.setCode(201);
            return baseResp;
        }
    }


    public Integer getId(HttpServletRequest request){
        CookieUtils cookiesUtils = new CookieUtils();
        String uuid = cookiesUtils.getToken(request);
        Object token = redisTemplate.opsForValue().get(uuid);
        JWTutils jwtUtils = new JWTutils();
        Map verifier = jwtUtils.Verifier(token.toString());
        Integer id1 = Integer.valueOf(verifier.get("id").toString());
        return id1;
    }
}
